use "${clean_data}\gfs_cleaned_merged_database.dta", clear
cd "${output}\"

global country_level_controls "v2x_polyarchy zwvs_traditional zwps_index lngdp"
global country_valid "zmort_rate"
global demo "male other_gender secondary tertiary hhinc  age_group2 age_group3 age_group4 age_group5 age_group6 age_group7 foreign_born  employed never_married married live_alone no_children one_child two_children  rural village suburb"
global parent_demo "parents_married living_structure2 living_structure3 living_structure4  child_poverty was_abused"
global extended_controls "parent_relig_index self_relig_index REincome_feelings"

foreach x in $country_level_controls {
gen X_`x'=`x'*parent_relig_index
gen REL_`x'=`x'*PCRQ
}

global interaction_religion "X_v2x_polyarchy X_zwvs_traditional X_zwps_index X_lngdp"
global interaction_pcrq "REL_v2x_polyarchy REL_zwvs_traditional REL_zwps_index REL_lngdp"
gen Xparent_relig_index_PCRQ=parent_relig_index*PCRQ

label var Xparent_relig_index_PCRQ "PCRQ X parental religiosity index"

label var X_v2x_polyarchy "Interaction-parental religiosity X electoral democracy"
label var  X_zwvs_traditional "Interaction-parental religiosity X secular values"
label var  X_zwps_index  "Interaction-parental religiosity X Women's rights"
label var  X_lngdp "Interaction-parental religiosity X GDP per capita"

label var REL_v2x_polyarchy "PCRQ X electoral democracy"
label var  REL_zwvs_traditional "PCRQ X secular values"
label var  REL_zwps_index  "PCRQ X Women's rights"
label var  REL_lngdp "Interact-PCRQ X GDP per capita"

gen pcrq_x_hap= HAPPY*PCRQ
label var pcrq_x_hap "Interact-PCRQ X Mental Health Index"

eststo clear

foreach Y in HOPE  {
eststo: quietly mixed `Y'  HAPPY  PCRQ $demo  [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace
eststo: quietly mixed `Y'   HAPPY PCRQ $demo $parent_demo  [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace

eststo: quietly mixed `Y'   HAPPY PCRQ $demo $parent_demo lngdp $extended_controls [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace
estadd local log_likelihood `e(ll)', replace

eststo: quietly mixed `Y'  HAPPY pcrq_x_hap PCRQ $demo  [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace
eststo: quietly mixed `Y'   HAPPY pcrq_x_hap PCRQ $demo $parent_demo  [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace

eststo: quietly mixed `Y'   HAPPY pcrq_x_hap PCRQ $demo $parent_demo lngdp $extended_controls [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace
estadd local log_likelihood `e(ll)', replace
}

esttab using "SUP_predict_wellbeing_memory_bias_model.csv", ///
stats(log_likelihood N, label("Log likelihood" "Sample Size")) ///
b(%15.3f) se(%15.3f) label  star(* 0.05 ** 0.01 *** 0.001)   nogaps replace

esttab using "SUP_predict_wellbeing_memory_bias_model_full_stats.csv", ///
stats(log_likelihood N k, label("Log likelihood" "Sample Size" "k number of parameters")) ///
cells(b(fmt(3)) t(fmt(3) par) ci_l(fmt(3))&ci_u(fmt(3)) p(fmt(4) par))  ///
nogaps   nolines  collabels(none)  star(* 0.05 ** 0.01 *** 0.001) label numbers replace ///
addnotes("rows correspond to beta values; t-statistics; 95% confidence intervals; p-values.")

*********
**Include health while growing up**
*****************
**reverse code **
****************
foreach  i in health_growup {
gen RE`i'=5 if `i'==1
replace RE`i'=4 if `i'==2
replace RE`i'=3 if `i'==3
replace RE`i'=2 if `i'==4
replace RE`i'=1 if `i'==5
}

label var REhealth_growup "Health while growing up"

gen pcrq_x_h_child= REhealth_growup*PCRQ
label var pcrq_x_h_child "Interact-PCRQ X Health as Child"

eststo clear

foreach Y in HOPE  {
eststo: quietly mixed `Y'  REhealth_growup  PCRQ $demo  [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace
eststo: quietly mixed `Y'   REhealth_growup PCRQ $demo $parent_demo  [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace

eststo: quietly mixed `Y'   REhealth_growup PCRQ $demo $parent_demo lngdp $extended_controls [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace
estadd local log_likelihood `e(ll)', replace

eststo: quietly mixed `Y'  REhealth_growup pcrq_x_h_child PCRQ $demo  [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace
eststo: quietly mixed `Y'   REhealth_growup pcrq_x_h_child PCRQ $demo $parent_demo  [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace

eststo: quietly mixed `Y'   REhealth_growup pcrq_x_h_child PCRQ $demo $parent_demo lngdp $extended_controls [pw=weight] || iso:, mle nolog vce(cluster iso)  
estadd local log_likelihood `e(ll)', replace
estadd local log_likelihood `e(ll)', replace
}

esttab using "SUP_predict_wellbeing_health_as_child_memory_bias_model.csv", ///
stats(log_likelihood N, label("Log likelihood" "Sample Size")) ///
b(%15.3f) se(%15.3f) label  star(* 0.05 ** 0.01 *** 0.001)   nogaps replace

esttab using "SUP_predict_wellbeing_health_as_child_memory_bias_model_full_stats.csv", ///
stats(log_likelihood N k, label("Log likelihood" "Sample Size" "k number of parameters")) ///
cells(b(fmt(3)) t(fmt(3) par) ci_l(fmt(3))&ci_u(fmt(3)) p(fmt(4) par))  ///
nogaps   nolines  collabels(none)  star(* 0.05 ** 0.01 *** 0.001) label numbers replace ///
addnotes("rows correspond to beta values; t-statistics; 95% confidence intervals; p-values.")
